﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Xml;

namespace dasBlog.Storage.Configuration
{
    public sealed class UriLayoutsSection : ConfigurationSection
    {
        // Fields
        private static ConfigurationPropertyCollection _properties = new ConfigurationPropertyCollection();
        private static readonly ConfigurationProperty _propUriLayouts = new ConfigurationProperty(null, typeof(UriLayoutSectionCollection), null, ConfigurationPropertyOptions.IsDefaultCollection);

        // Methods
        static UriLayoutsSection()
        {
            _properties.Add(_propUriLayouts);
        }

        // Properties
        protected override ConfigurationPropertyCollection Properties
        {
            get
            {
                return _properties;
            }
        }

        [ConfigurationProperty("", IsDefaultCollection = true)]
        public UriLayoutSectionCollection UriLayouts
        {
            get
            {
                return (UriLayoutSectionCollection)base[_propUriLayouts];
            }
        }
    }


    public sealed class UriLayoutSectionCollection : ConfigurationElementCollection
    {
        // Methods
        public void Add(UriLayoutSection element)
        {
            this.BaseAdd(element);
        }

        public void Clear()
        {
            base.BaseClear();
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new UriLayoutSection();
        }

        public UriLayoutSection Get(string elementKey)
        {
            return (UriLayoutSection)base.BaseGet(elementKey);
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((UriLayoutSection)element).Key;
        }

        public void Remove(UriLayoutSection element)
        {
            base.BaseRemove(this.GetElementKey(element));
        }

        // Properties
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }

        protected override string ElementName
        {
            get
            {
                return "uriLayout";
            }
        }
    }


    public sealed class UriLayoutSection : ConfigurationSection
    {
        // Fields
        private static ConfigurationPropertyCollection _properties = 
            new ConfigurationPropertyCollection();
        private static readonly ConfigurationProperty _propSegments = 
            new ConfigurationProperty("", typeof(SegmentElementCollection), null, 
                ConfigurationPropertyOptions.IsDefaultCollection);
        private static readonly ConfigurationProperty _propName =
            new ConfigurationProperty("name", typeof(string), "",
                ConfigurationPropertyOptions.IsKey | ConfigurationPropertyOptions.IsRequired);
        
        // Methods
        static UriLayoutSection()
        {
            _properties.Add(_propSegments);
            _properties.Add(_propName);
        }

        public UriLayoutSection()
        {

        }

        // Properties
        protected override ConfigurationPropertyCollection Properties
        {
            get
            {
                return _properties;
            }
        }

        [ConfigurationProperty("name", IsRequired = true, IsKey = true, DefaultValue = "")]
        public string Name
        {
            get
            {
                return (string)base[_propName];
            }
            set
            {
                base[_propName] = value;
            }
        }
        
        // Properties
        internal string Key
        {
            get
            {
                return this.Name;
            }
        }


        [ConfigurationProperty("", IsDefaultCollection = true)]
        public SegmentElementCollection Segments
        {
            get
            {
                return (SegmentElementCollection)base[_propSegments];
            }
        }

    }

    public sealed class SegmentElementCollection : ConfigurationElementCollection
    {
        // Methods
        public void Add(SegmentElement element)
        {
            this.BaseAdd(element);
        }

        public void Clear()
        {
            base.BaseClear();
        }
        

        protected override ConfigurationElement CreateNewElement()
        {
            return new SegmentElement();
        }

        public SegmentElement Get(string elementKey)
        {
            return (SegmentElement)base.BaseGet(elementKey);
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((SegmentElement)element).Key;
        }

        public void Remove(SegmentElement element)
        {
            base.BaseRemove(this.GetElementKey(element));
        }

        // Properties
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }

        protected override string ElementName
        {
            get
            {
                return "segment";
            }
        }

        protected override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
        {
            base.DeserializeElement(reader, serializeCollectionKey);
        }

        protected override bool SerializeElement(XmlWriter writer, bool serializeCollectionKey)
        {
            return base.SerializeElement(writer, serializeCollectionKey);
        }        
    }

    public sealed class SegmentElement : ConfigurationSection
    {
        // Fields
        private static ConfigurationPropertyCollection _properties =
            new ConfigurationPropertyCollection();
        private static readonly ConfigurationProperty _propSegments =
            new ConfigurationProperty(null, typeof(SegmentElementCollection), null,
                ConfigurationPropertyOptions.IsDefaultCollection);
        private static readonly ConfigurationProperty _propRole =
            new ConfigurationProperty("role", typeof(string), "",
                ConfigurationPropertyOptions.IsKey | ConfigurationPropertyOptions.IsRequired);
        private static readonly ConfigurationProperty _propNode =
            new ConfigurationProperty("node", typeof(string), "",
                ConfigurationPropertyOptions.None);
        private static readonly ConfigurationProperty _propRecursive =
            new ConfigurationProperty("recursive", typeof(bool), false,
                ConfigurationPropertyOptions.None);
        
        // Methods
        static SegmentElement()
        {
            _properties.Add(_propRole);
            _properties.Add(_propNode);
            _properties.Add(_propSegments);
            _properties.Add(_propRecursive);
        }

        public SegmentElement()
        {
        }

        public SegmentElement(string role, string node)
            : this()
        {
            this.Role = role;
            this.Node = node;
        }

        public override bool Equals(object providerCredentials)
        {
            SegmentElement element = providerCredentials as SegmentElement;
            if ((element != null) && base.Equals(providerCredentials))
            {
                return object.Equals(this.Role, element.Role);
            }
            return false;
        }

        public override int GetHashCode()
        {
            return (base.GetHashCode() ^ this.Node.GetHashCode());
        }

        // Properties
        internal string Key
        {
            get
            {
                return this.Role;
            }
        }

        [ConfigurationProperty("role", IsRequired = true, IsKey = true, DefaultValue = "")]
        public string Role
        {
            get
            {
                return (string)base[_propRole];
            }
            set
            {
                base[_propRole] = value;
            }
        }

        protected override ConfigurationPropertyCollection Properties
        {
            get
            {
                return _properties;
            }
        }

        [ConfigurationProperty("node", IsRequired = true, DefaultValue = null)]
        public string Node
        {
            get
            {
                return (string)base[_propNode];
            }
            set
            {
                base[_propNode] = value;
            }
        }

        [ConfigurationProperty("recursive", IsRequired = false, DefaultValue = false)]
        public bool Recursive
        {
            get
            {
                return (bool)base[_propRecursive];
            }
            set
            {
                base[_propRecursive] = value;
            }
        }

        
        [ConfigurationProperty("", IsDefaultCollection = true)]
        public SegmentElementCollection Segments
        {
            get
            {
                return (SegmentElementCollection)base[_propSegments];
            }
        }

    }

}
